﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using FoundationClass.Entity;

namespace FoundationClass.Data
{
    public class ServiceRecordData
    {
        public static void InsertServiceRecord(ServiceRecord serviceRecord)
        {
            try
            {
                DataTable serviceRecordItemDt = DatabaseManager.Read("ServiceRecord");
                int newId = GetMaxServiceRecordId() + 1;
                serviceRecord.Id = newId;

                serviceRecordItemDt.Rows.Add(newId, serviceRecord.RecordStart, string.Empty, serviceRecord.RecordStatus, string.Empty, serviceRecord.Computer.Id, serviceRecord.Promotion.Id);
                DatabaseManager.Write(serviceRecordItemDt);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private static int GetMaxServiceRecordId()
        {
            DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
            int rowcount = serviceRecordDt.Rows.Count;
            if (rowcount == 0)
                return 1;
            else
                return int.Parse(serviceRecordDt.Rows[rowcount - 1]["recordid"].ToString());
        }

        public static void UpdateServiceRecord(ServiceRecord serviceRecord)
        {
            try
            {
                DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
                foreach (DataRow row in serviceRecordDt.Rows)
                {
                    if (row["recordid"].Equals(serviceRecord.Id.ToString()))
                    {
                        row["recordstart"] = serviceRecord.RecordStart;
                        row["recordend"] = serviceRecord.RecordEnd;
                        row["recordstatus"] = serviceRecord.RecordStatus;
                        row["recordamount"] = serviceRecord.RecordAmount;
                        row["computerid"] = serviceRecord.Computer.Id;
                        row["paymentid"] = serviceRecord.Payment == null ? string.Empty : serviceRecord.Payment.PaidId.ToString();

                        break;
                    }
                }
                DatabaseManager.Write(serviceRecordDt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void ChangeComputer(int recordId, int computerId)
        {
            DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
            foreach (DataRow row in serviceRecordDt.Rows)
            {
                if (row["recordid"].Equals(recordId.ToString()))
                {
                    row["computerid"] = computerId;

                    break;
                }
            }
            DatabaseManager.Write(serviceRecordDt);
        }

        public static void DeleteServiceRecord(int id)
        {
            try
            {
                DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
                if (serviceRecordDt == null)
                    return;
                foreach (DataRow row in serviceRecordDt.Rows)
                {
                    if (row["recordid"].Equals(id.ToString()))
                    {
                        DataTable serviceRecordItemDt = DatabaseManager.Read("ServiceRecordItem");
                        foreach (DataRow irow in serviceRecordItemDt.Rows)
                        {
                            if (irow["recordid"].Equals(id))
                            {
                                irow.Delete();
                                break;
                            }
                        }
                        DatabaseManager.Write(serviceRecordItemDt);
                        row.Delete();
                        break;
                    }
                }
                DatabaseManager.Write(serviceRecordDt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static ServiceRecord GetServiceRecord(int id)
        {
            try
            {
                ServiceRecord serviceRecord = null;
                DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
                if (serviceRecordDt == null)
                    return null;
                foreach (DataRow row in serviceRecordDt.Rows)
                {
                    if ((row["recordid"]).Equals(id.ToString()))
                    {
                        serviceRecord = new ServiceRecord()
                        {
                            Id = int.Parse((string)row["recordid"]),
                            RecordStart = string.IsNullOrEmpty(row["recordstart"] as string) ? DateTime.Now : DateTime.Parse(row["recordstart"].ToString()),
                            RecordEnd = string.IsNullOrEmpty(row["recordend"] as string) ? (DateTime?)null : DateTime.Parse(row["recordend"].ToString()),
                            RecordStatus = int.Parse((string)row["recordstatus"]),
                            RecordAmount = (string)row["recordamount"],
                            Computer = ComputerData.GetComputer(int.Parse((string)row["computerid"])),
                            Payment = string.IsNullOrEmpty(row["paymentid"].ToString()) ? null : PaymentData.GetPayment(int.Parse((string)row["paymentid"])),
                            Promotion = PromotionData.GetPromotion(int.Parse((string)row["promotionid"]))
                        };
                        break;
                    }
                }
                return serviceRecord;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ServiceRecord> GetServiceRecords()
        {
            try
            {
                var list = new List<ServiceRecord>();

                DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
                if (serviceRecordDt == null)
                    return list;
                foreach (DataRow row in serviceRecordDt.Rows)
                {
                    var serviceRecord = new ServiceRecord();
                    serviceRecord.Id = int.Parse((string)row["recordid"]);
                    if (!string.IsNullOrEmpty(row["recordstart"] as string))
                        serviceRecord.RecordStart = DateTime.Parse(row["recordstart"].ToString());
                    if (!string.IsNullOrEmpty(row["recordend"] as string))
                        serviceRecord.RecordEnd = DateTime.Parse(row["recordend"].ToString());
                    if (!string.IsNullOrEmpty(row["recordstatus"] as string))
                        serviceRecord.RecordStatus = int.Parse((string)row["recordstatus"]);
                    if (!string.IsNullOrEmpty(row["recordamount"] as string))
                        serviceRecord.RecordAmount = (string)row["recordamount"];
                    if (!string.IsNullOrEmpty(row["computerid"] as string))
                    {
                        serviceRecord.Computer = ComputerData.GetComputer(int.Parse((string)row["computerid"]));
                    }
                    if (!string.IsNullOrEmpty(row["paymentid"] as string))
                        serviceRecord.Payment = PaymentData.GetPayment(int.Parse((string)row["paymentid"]));
                    if (!string.IsNullOrEmpty(row["promotionid"] as string))
                        serviceRecord.Promotion = PromotionData.GetPromotion(int.Parse((string)row["promotionid"]));
                    list.Add(serviceRecord);
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ServiceRecord> GetServiceRecordsAvaliable()
        {
            try
            {
                var list = new List<ServiceRecord>();

                DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
                if (serviceRecordDt == null)
                    return list;
                foreach (DataRow row in serviceRecordDt.Rows)
                {
                    if (int.Parse((string)row["recordstatus"]) != 0)
                    {var serviceRecord = new ServiceRecord();
                        serviceRecord.Id = int.Parse((string)row["recordid"]);
                        if (!string.IsNullOrEmpty(row["recordstart"] as string))
                            serviceRecord.RecordStart = DateTime.Parse(row["recordstart"].ToString());
                        if (!string.IsNullOrEmpty(row["recordend"] as string))
                            serviceRecord.RecordEnd = DateTime.Parse(row["recordend"].ToString());
                        if (!string.IsNullOrEmpty(row["recordstatus"] as string))
                            serviceRecord.RecordStatus = int.Parse((string)row["recordstatus"]);
                        if (!string.IsNullOrEmpty(row["recordamount"] as string))
                            serviceRecord.RecordAmount = (string)row["recordamount"];
                        if (!string.IsNullOrEmpty(row["computerid"] as string))
                        {
                            serviceRecord.Computer = ComputerData.GetComputer(int.Parse((string)row["computerid"]));
                        }
                        if (!string.IsNullOrEmpty(row["paymentid"] as string))
                            serviceRecord.Payment = PaymentData.GetPayment(int.Parse((string)row["paymentid"]));
                        if (!string.IsNullOrEmpty(row["promotionid"] as string))
                            serviceRecord.Promotion = PromotionData.GetPromotion(int.Parse((string)row["promotionid"]));
                        list.Add(serviceRecord);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ServiceRecord> GetServiceRecordsPaid()
        {
            try
            {
                var list = new List<ServiceRecord>();

                DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
                if (serviceRecordDt == null)
                    return list;
                foreach (DataRow row in serviceRecordDt.Rows)
                {
                    if (int.Parse((string)row["recordstatus"]) == 0)
                    {
                        var serviceRecord = new ServiceRecord();
                        serviceRecord.Id = int.Parse((string)row["recordid"]);
                        if (!string.IsNullOrEmpty(row["recordstart"] as string))
                            serviceRecord.RecordStart = DateTime.Parse(row["recordstart"].ToString());
                        if (!string.IsNullOrEmpty(row["recordend"] as string))
                            serviceRecord.RecordEnd = DateTime.Parse(row["recordend"].ToString());
                        if (!string.IsNullOrEmpty(row["recordstatus"] as string))
                            serviceRecord.RecordStatus = int.Parse((string)row["recordstatus"]);
                        if (!string.IsNullOrEmpty(row["recordamount"] as string))
                            serviceRecord.RecordAmount = (string)row["recordamount"];
                        if (!string.IsNullOrEmpty(row["computerid"] as string))
                        {
                            serviceRecord.Computer = ComputerData.GetComputer(int.Parse((string)row["computerid"]));
                        }
                        if (!string.IsNullOrEmpty(row["paymentid"] as string))
                            serviceRecord.Payment = PaymentData.GetPayment(int.Parse((string)row["paymentid"]));
                        if (!string.IsNullOrEmpty(row["promotionid"] as string))
                            serviceRecord.Promotion = PromotionData.GetPromotion(int.Parse((string)row["promotionid"]));
                        list.Add(serviceRecord);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<ServiceRecord> GetServiceRecordsClose()
        {
            try
            {
                var list = new List<ServiceRecord>();

                DataTable serviceRecordDt = DatabaseManager.Read("ServiceRecord");
                if (serviceRecordDt == null)
                    return list;
                foreach (DataRow row in serviceRecordDt.Rows)
                {
                    if (int.Parse((string)row["recordstatus"]) == 2)
                    {
                        var serviceRecord = new ServiceRecord();
                        serviceRecord.Id = int.Parse((string)row["recordid"]);
                        if (!string.IsNullOrEmpty(row["recordstart"] as string))
                            serviceRecord.RecordStart = DateTime.Parse(row["recordstart"].ToString());
                        if (!string.IsNullOrEmpty(row["recordend"] as string))
                            serviceRecord.RecordEnd = DateTime.Parse(row["recordend"].ToString());
                        if (!string.IsNullOrEmpty(row["recordstatus"] as string))
                            serviceRecord.RecordStatus = int.Parse((string)row["recordstatus"]);
                        if (!string.IsNullOrEmpty(row["recordamount"] as string))
                            serviceRecord.RecordAmount = (string)row["recordamount"];
                        if (!string.IsNullOrEmpty(row["computerid"] as string))
                        {
                            serviceRecord.Computer = ComputerData.GetComputer(int.Parse((string)row["computerid"]));
                        }
                        if (!string.IsNullOrEmpty(row["paymentid"] as string))
                            serviceRecord.Payment = PaymentData.GetPayment(int.Parse((string)row["paymentid"]));
                        if (!string.IsNullOrEmpty(row["promotionid"] as string))
                            serviceRecord.Promotion = PromotionData.GetPromotion(int.Parse((string)row["promotionid"]));
                        list.Add(serviceRecord);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

}
